export const timeDown = (time) => {
    let minutes = 0, seconds = 0;
    if (time >= 0) {
        minutes = Math.floor(time / 60);
        seconds = Math.floor(time % 60);
        
    } else {
        time = 0;
    }
    minutes = minutes + '分';
    seconds = seconds < 10 ? '0' + seconds + '秒' : seconds + '秒';
    let str = minutes + seconds;
    return str;
};

export const formatDate = (time) => {
    time = new Date(parseInt(time * 1000));
    const hours = time.getHours();
    const second = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    return `${hours}:${second}`;
};

export const formatHour = (time) => {
    time = new Date(parseInt(time * 1000));
    const hours = time.getHours();
    return hours + '点';
};

export const changeParams = (param) => {
    return JSON.stringify(param)
      .replace(/:/g,'=')
      .replace(/,/g,'&')
      .replace(/{/g,'')
      .replace(/}/g,'')
      .replace(/"/g,'');
};

export const hasClass = (el, cls) => {
    if (!el || !cls) return false;
    if (cls.indexOf(' ') !== -1) {
        throw new Error('className should not contain space.');
    }
    if (el.classList) {
        return el.classList.contains(cls);
    } else {
        return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
    }
};

export const removeClass = (el, cls) => {
    if (!el || !cls) return false;
    const classes = cls.split(' ');
    let curClass = ' ' + el.className + ' ';
    for (let i = 0; i < classes.length; i++) {
        let clsName = classes[i];
        if (!clsName) continue;
        if (el.classList) {
        el.classList.remove(clsName);
        } else {
        if (hasClass(el, clsName)) {
            curClass = curClass.replace(' ' + clsName + ' ', ' ');
        }
        }
    }
    if (!el.classList) {
        el.className = trim(curClass);
    }
};

export const addClass = (el, cls) => {
    if (!el) return false;
    let curClass = el.className;
    const classes = (cls || '' ).split(' ');
    for (let i = 0; i < classes.length; i++) {
        const clsName = classes[i];
        if (!clsName) continue;
        if (el.classList) {
        el.classList.add(clsName);
        } else {
        if (!hasClass(el, clsName)) {
            curClass += ' ' + clsName;
        }
        }
    }
    if (!el.classList) {
        el.className = curClass;
    }
};